#import "CapitalsGame.h"

@interface CapitalsGame ()


@property BOOL isGameOver;
@property NSArray* colors;
@property NSMutableDictionary* answers;
@property NSArray* keys;
@property NSInteger maxNumberOfWrongAnswers;
@property NSString* currentCapital;
@property NSString* currentCountry;

@end


@implementation CapitalsGame


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setName:@"CapitalGame"];
        [self setType:knowleadge];
        self.ruleOfTheGame=@"Is the capital correct?";
        self.answers=[[NSMutableDictionary alloc]init];
        [self.answers setObject:@"Sofia" forKey:@"Bulgaria"];
        [self.answers setObject:@"Paris" forKey:@"France"];
        [self.answers setObject:@"Washington" forKey:@"USA"];
        [self.answers setObject:@"Brasília" forKey:@"Brazil"];
        [self.answers setObject:@"London" forKey:@"UK"];
        [self.answers setObject:@"Berlin" forKey:@"Germany"];
        [self.answers setObject:@"Madrid" forKey:@"Spain"];
        [self.answers setObject:@"Brussels" forKey:@"Belgium"];
        [self.answers setObject:@"Stockholm" forKey:@"Sweden"];
        [self.answers setObject:@"Lisbon" forKey:@"Portugal"];
        
        
        self.keys=[self.answers allKeys];
        self.colors=[[NSArray alloc]initWithObjects:@"blue",@"white",@"red",@"yellow",@"orange",@"purple",@"green", nil];
        
        self.maxNumberOfWrongAnswers=arc4random_uniform(kMaxNumberOfWrongAnswer);
    }
    return self;
}



-(void)draw{
    [self removeAllComponents];
    
    int firstNumber=arc4random_uniform(kPopulationGameNumberOfCountry);
    int secondNumber=arc4random_uniform(kPopulationGameNumberOfCountry);

    
    int colour=arc4random_uniform(kColorNumber);
    
    
    self.currentCountry=self.keys[firstNumber];
    NSString* capitalKey=self.keys[secondNumber];

    
    if (self.maxNumberOfWrongAnswers == 0) {
        self.currentCapital=[self.answers objectForKey:self.currentCountry];
        self.maxNumberOfWrongAnswers=arc4random_uniform(3);
    }
    else{
        self.currentCapital=[self.answers objectForKey:capitalKey];
        self.maxNumberOfWrongAnswers--;
    }
    
    
    NSString* labelText=[NSString stringWithFormat:@"%@ -> %@",self.currentCountry,self.currentCapital];
    NSString* labelColor=[NSString stringWithFormat:@"%@Color",[self.colors objectAtIndex:colour]];
    
    
    
    
    LabelGameComponent* label=[[LabelGameComponent alloc]initWithText:labelText andFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight/kHeighDel) andFontSize:(kScreenWidth/kWidthDel)/2 andColour:labelColor];
    LabelGameComponent* labelOne=[[LabelGameComponent alloc]initWithText:labelText andFrame:CGRectMake(0, kScreenHeight-kScreenHeight/kHeighDel - 2*kButtonHeigh, kScreenWidth, kScreenHeight/kHeighDel) andFontSize:(kScreenWidth/kWidthDel)/2 andColour:labelColor];

    
    [self addComponents:label];
    [self addComponents:labelOne];
    
}



-(BOOL)isRight{
    if ([self.currentCapital isEqualToString:[self.answers objectForKey:self.currentCountry]]) {
        return YES;
        
    }
    else{
        return NO;
    }
}

-(void)play{
    [self draw];
    
    [NSTimer scheduledTimerWithTimeInterval: 2                                                 target: self selector:@selector(newCapital:)userInfo: nil repeats:YES];
    
}

-(void)newCapital:(NSTimer*)timer{
    if (self.isGameOver==YES) {
        [timer invalidate];
    }
    else{
        [self draw];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"SomeGames"
         object:self userInfo:nil];
        
    }
    
}

-(void)gameOver{
    self.isGameOver=YES;
}


@end
